<template>
  <div>
    <el-descriptions class="margin-top" title="商品明细" :column="3" border>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">商品ID</div>
        </template>
        {{ prod.prodId }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">商品名称</div>
        </template>
        {{ prod.prodName }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">SPU</div>
        </template>
        {{ prod.prodSpu }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">图片</div>
        </template>
        <el-image
          :src="prod.prodImage"
          style="width: 50px; height: 50px"
        ></el-image>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        {{ prod.createTime }}
      </el-descriptions-item>
    </el-descriptions>
  </div>
  <!-- 明细列表 -->
  <h1>商品SKU</h1>
  <el-button type="primary" @click="onAddSku">添加</el-button>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="infoId" label="ID" />
      <el-table-column prop="prodSku" label="SKU" />
      <el-table-column prop="prodName" label="商品名称" />
      <el-table-column prop="prodPrice" label="商品售价" />
      <el-table-column prop="costPrice" label="成本价" />
      <el-table-column prop="prodStock" label="库存" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="address" label="操作">
        <el-button type="primary">编辑</el-button>
      </el-table-column>
    </el-table>
  </div>
  <!-- 分页 -->
  <div style="margin-top: 20px">
    <div class="demo-pagination-block" style="width: 800px; margin: 0 auto">
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <!-- 添加商品SKU -->
  <div>
    <el-dialog v-model="dialogFormVisible" title="" width="500">
      <el-form :model="form">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="form.prodName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品SKU" :label-width="formLabelWidth">
          <el-input v-model="form.prodSku" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品成本价" :label-width="formLabelWidth">
          <el-input v-model="form.costPrice" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品售价" :label-width="formLabelWidth">
          <el-input v-model="form.prodPrice" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品库存" :label-width="formLabelWidth">
          <el-input v-model="form.prodStock" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addInfo"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useRoute } from "vue-router";
import { ref } from "vue";

import { getProdById, getProdInfoListAll, addProdInfo } from "@/api/api";
import { onMounted } from "vue";
import { ElMessage } from "element-plus";
const router = useRoute();
const prodId = router.params.prodId;
const total = ref(0);
const prod = ref({
  prodId: null,
  prodSpu: null,
  prodKind: null,
  prodType: null,
  prodStatus: null,
  prodName: null,
  brandId: null,
  unitId: null,
  tagId: null,
  appr: null,
  prodImage: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  kindName: null,
  brandName: null,
  unitName: null,
  tagName: null,
  typeName: null,
});

const tableData = ref([]);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  prodId: prodId,
});
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  buyNum: null,
  costPrice: null,
  createBy: null,
  createTime: null,
  infoId: null,
  prodId: null,
  prodName: null,
  prodPrice: null,
  prodSku: null,
  prodStock: null,
  updateBy: null,
  updateTime: null,
});
//提交表单
const addInfo = () => {
  addProdInfo(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//添加商品
const onAddSku = () => {
  form.value = {
    buyNum: null,
    costPrice: null,
    createBy: null,
    createTime: null,
    infoId: null,
    prodId: null,
    prodName: null,
    prodPrice: null,
    prodSku: null,
    prodStock: null,
    updateBy: null,
    updateTime: null,
  };
  form.value.prodId = prodId;
  dialogFormVisible.value = true;
};

//分页回调
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
const loadList = () => {
  getProdInfoListAll(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
onMounted(() => {
  loadList();
  getProdById(prodId).then((res) => {
    if (res.data.code == 200) {
      prod.value = res.data.data;
    }
  });
});
</script>

<style scoped></style>
